package com.msnrobotweb;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import net.sf.jml.MsnContact;
import net.sf.jml.MsnMessenger;
import net.sf.jml.MsnSwitchboard;
import net.sf.jml.event.MsnMessageAdapter;
import net.sf.jml.impl.MsnMessengerFactory;
import net.sf.jml.message.MsnControlMessage;
import net.sf.jml.message.MsnDatacastMessage;
import net.sf.jml.message.MsnInstantMessage;

import org.simpleframework.http.Container;
import org.simpleframework.http.connect.Connection;
import org.simpleframework.http.connect.SocketConnection;

import com.msnrobotweb.http.RobotServer;

/**
 * Robot messenger
 * @author bm888888@gmail.com
 */
public class RobotMessenger  {
    
    private String email;
    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    protected void initMessenger(MsnMessenger messenger) {
        messenger.addMessageListener(new MsnMessageAdapter() {

            public void instantMessageReceived(MsnSwitchboard switchboard, MsnInstantMessage message, MsnContact contact) {
                // text message received
                message.setContent("sorry,I am fall in love, couldn't response your request");
                switchboard.sendMessage(message);
            }

            public void controlMessageReceived(MsnSwitchboard switchboard, MsnControlMessage message, MsnContact contact) {
                // such as typing message and recording message
                switchboard.sendMessage(message);
            }

            public void datacastMessageReceived(MsnSwitchboard switchboard, MsnDatacastMessage message, MsnContact contact) {
                // such as Nudge
                switchboard.sendMessage(message);
            }

        });

        try {
            int port = 9008;
            Container container = new RobotServer(messenger);
            Connection connection = new SocketConnection(container);
            SocketAddress address = new InetSocketAddress(port);
            connection.connect(address);

            System.out.print("http server started with port:" + port);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    public void start() {
        //create MsnMessenger instance
        MsnMessenger messenger = MsnMessengerFactory.createMsnMessenger(email,
                password);

        //MsnMessenger support all protocols by default
        //messenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP8 });

        //log incoming message
        messenger.setLogIncoming(false);

        //log outgoing message
        messenger.setLogOutgoing(false);

        initMessenger(messenger);
        messenger.login();
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("Usage: java RobotMessenger email password");
            return;
        }
        RobotMessenger messenger = new RobotMessenger();
        messenger.setEmail(args[0]);
        messenger.setPassword(args[1]);
        messenger.start();
    }

}
